<?php

include('../DB.php');

// 添加至 gearman worker 处理

class Client
{
    private $options;
    private $queueDB;
    private $writeDB;
    private $readDB;
    private $info;

    public function __construct($argv)
    {
        $dbConfig = include('../dbconfig.php');

        $this->parseArgv($argv);
        $this->connDB($dbConfig);
    }

    public function parseArgv($argv)
    {
        $shortOpts = "v::h::l:";
        $longOpts = ["verbose::", "help::", "limit:"];
        $options = getopt($shortOpts, $longOpts);

        foreach ($options as $k => $v) {
            if ($k=='h' || $k=='help') {
                $this->help();
            }
            if ($k=='l' || $k=='limit') {
                $this->options['limit'] = $v;
            }
            if ($k== 'v' || $k== 'verbose') {
                $this->info = true;
            }
        }
    }

    public function connDB($dbConfig)
    {
        $config = $dbConfig['queue'];
        $this->queueDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['write'];
        $this->writeDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['read'];
        $this->readDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);
    }

    public function help()
    {
        echo 'help info...';
    }

    public function run()
    {
        // 更新数据库队列的更新时间

        // 查询队列的数据
        $client= new GearmanClient();
        $client->addServer();

        $tasks = [

        ];
        foreach ($tasks as $task) {
            $workload = array(
                'url'   =>  $task['url'],
                'queue_review_id' => $task['queue_id'],
                'retry' => $task['retry'],
            );

            $client->doBackground("crawl_page", serialize($workload));
        }
    }
}

$client = new Client($argv);
$client->run();
